package org.rsbot.script.randoms;

import java.awt.Point;

import org.rsbot.script.Calculations;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.wrappers.RSInterfaceChild;
import org.rsbot.script.wrappers.RSObject;

/**
 * @author Secret Spy
 * @version 2.9 - 02/24/11
 */
@ScriptManifest(authors = { "Secret Spy" }, name = "Pinball", version = 2.9)
public class Pinball extends Random {

	private static final int[] OBJ_PILLARS = { 15000, 15002, 15004, 15006,
			15008 };

	private static final int[] OBJ_ACTIVATE = { 15000, 15002, 15004, 15006,
			15007, 15008 };

	private static final int INTERFACE_PINBALL = 263;

	// private int continueCounter = 0;

	@Override
    public boolean activateCondition() {
		return isLoggedIn() && getNearestObjectByID(OBJ_ACTIVATE) != null;
	}

	private int getScore() {
		final RSInterfaceChild score = getInterface(INTERFACE_PINBALL)
				.getComponent(1);
		try {
			return Integer.parseInt(score.getText().split(" ")[1]);
		} catch (final java.lang.ArrayIndexOutOfBoundsException t) {
			return 10;
		}
	}

	@Override
    public int loop() {
		setCameraAltitude(true);
		if (!activateCondition())
			return -1;
		//
		// if (interfaces.canContinue() && continueCounter < 10) {
		// log.info("trying to continue");
		// interfaces.clickContinue();
		// continueCounter++;
		//
		// log.info("Clicked continue");
		// return random(1000, 1200);
		// }
		// continueCounter = 0;

		if (getMyPlayer().isMoving() || getMyPlayer().getAnimation() != -1)
            return random(200, 300);

		if (getScore() >= 10) {
			final int OBJ_EXIT = 15010;
			final RSObject exit = getNearestObjectByID(OBJ_EXIT);
			if (exit != null) {
				if (tileOnScreen(exit.getLocation()) && atObject(exit, "Exit")) {
					sleep(random(2000, 2200));
					atObject(exit, "Exit");
					return random(1000, 1200);
				} else {
					setCompass('s');
					walkTileOnScreen(exit.getLocation());
					return random(1400, 1500);
				}
			}
		}

		final RSObject pillar = getNearestObjectByID(OBJ_PILLARS);

		if (pillar != null) {
			// log("Found pillar: " + pillar.getID() + " - " +
			// pillar.getLocation());
			if (!pillar.isOnScreen()) {
				walkTileOnScreen(pillar.getLocation());
				return random(200, 2500);
			}

			wait(random(300, 500));

			if (!atPillar(pillar))
				return random(500, 1000);
            else {
                wait(random(500, 1000));
            }
			final int before = getScore();
			for (int i = 0; i < 100; i++) {
				if (getScore() > before)
					return random(50, 100);
				wait(random(25, 75));
			}
			return random(800, 1100);
		}
		return random(200, 400);
	}
	
	public boolean atPillar(final RSObject pillar) {
		final String action = "Tag";
		if (!pillar.isOnScreen())
			return false;
		
		int movements = 0;
		
		while (movements < 2) {
			final Point p = getPointOnTop(pillar);
			if (p.x != -1 && p.y != -1) {
				moveMouse(p);
			}
			movements++;
		}
		
		return atMenu(action);
	}
	
	public Point getPointOnTop(final RSObject pillar) {
		return calc.tileToScreen(pillar.getLocation(), random(-550, -700));
	}
}